package de.listrong.da.main;

import de.listrong.da.metainfo.datamodel.MetaInfo;
import de.listrong.da.metainfo.datamodel.MetaInfoList;
import gate.Corpus;
import gate.creole.ResourceInstantiationException;
import org.apache.log4j.Logger;

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

/* Main Class for Program.
 * @author Qiang
 */

public class Main {
    
	public static Logger myLogger = Logger.getLogger(Main.class);

	/**
	 * The program begin from here.
	 * 
	 * @param args - direction path of document corpus. 
	 * @throws IOException, ResourceInstantiationException
	 */
	public static void main(String[] args) throws IOException,
			ResourceInstantiationException {
        
        ///////////////////////////////////////////////////////////////////////////////////////
        myLogger.info("using default arguments \"y\" for showGUI \"E:\\docrsc\" for document corpus folder");

        List<String> fileList = new ArrayList<String>();
		File docFolder = new File("E:\\docrsc");
		if ( docFolder.exists() && docFolder.isDirectory() ) {
			File[] fileLst = docFolder.listFiles();
			for( File currentFileObject:fileLst ){
				if ( !currentFileObject.isDirectory()) {
					String fileURLString = currentFileObject.getAbsoluteFile().toURI().toString();
					fileList.add(fileURLString);
				} // end if
			} // end for
		} // end if
        
        ///////////////////////////////////////////////////////////////////////////////////////
        /* begin to generate the MetaInfo for Document Source*/
        myLogger.info("load the meta informations from file metainfos.xml for test doucments");
        File metaFile = new File("E:\\workspace34\\obit\\src\\main\\resources\\metainfos.xml");
        HashMap<String, MetaInfo> metaInfoList = MetaInfoList.readFile(metaFile);
        /* end of MetaInfor generation */

        ///////////////////////////////////////////////////////////////////////////////////////
        /* begin of GATE application */
        myLogger.info("Begin of information extraction with GATE");
        GateProcessBuilder myGateProcess = new GateProcessBuilder(fileList, true);
        try {
            myGateProcess.build();
            myGateProcess.excute();
        } catch (Exception e) {
            e.printStackTrace();  
            myLogger.error("Error by myGateProcess.build()");
        }
        Corpus docCorpus = myGateProcess.getDocumentCorpus();
		/* end of GATE application */
		
		///////////////////////////////////////////////////////////////////////////////////////
		/*
		 * here will the ABox be generated in OWL format and write into the file system.
		 * Each document in document corpus is order to one OWL file.
		 */
        myLogger.info("Begin to generate ABoxes");
		OntologyProcess.aboxGenerator(docCorpus, metaInfoList);

	}
}
